package com.wavehi.consumer;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.wavehi.util.RabbitMQFactoryUtils;
import com.wavehi.util.SleepUtils;

/**
 * @Description 消费者
 * @Author WaveHi
 * @Date 24/12/2021 下午 4:54
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 */
public class Consumer_05 {

    private static final String QUEUE_NAME = "ack_queue";

    public static void main(String[] args) throws Exception {

        //1.创建连接信道
        Channel channel = RabbitMQFactoryUtils.getChannel();
        //2.接收消息
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            SleepUtils.sleep(5);
            System.out.println("Message :" + new String(message.getBody(), "UTF-8"));
            channel.basicAck(message.getEnvelope().getDeliveryTag(),false);
        };
        CancelCallback cancelCallback = consumerTag -> {
            System.out.println("Message getting stop!");
        };
        channel.basicConsume(QUEUE_NAME, false, deliverCallback, cancelCallback);
        System.out.println("Consumer_05 waits for message!");
    }
}
